﻿package Server.AHMJ.YJMJ;

import java.util.HashMap;
import java.util.Map;

import Server.Agent.GameAgent;
import Server.Model.Base.IRoom;
import Server.Model.Base.SHMUser;
import Server.Model.Hall.QpGameMgr;
import Server.Model.Hall.dthall;
import common.routine.GlobalSetting;
import net.sf.json.JSONObject;

public class MajTableMgr {
	static MajTableMgr __instance = new MajTableMgr();

	public static MajTableMgr instance() {
		return __instance;
	}

	Long m_LastDealTime;
	Long m_LastUpdateTime;

	static Map<Integer, MajTable> m_mapTable = new HashMap<>();// 房间号 . 房间
	Map<Integer, Long> m_mapHeartTime = new HashMap<>();// 心跳时间

	void Init() {
		m_LastDealTime = 0L;
		m_LastUpdateTime = 0L;
	}

	static MajTable FindTable(int _Uid) {
		MajTable it = (MajTable) QpGameMgr.getInstance().getUserRoomByUid(_Uid);
		if (it != null) {
			return it;
		}
		return null;
	}

	void Handle_onHeart(int _Uid) {

	}

	public static void Handle_onConnect(GameAgent agent, String[] vecArg) {
		
		SUBCMD_S_Connect cmd = new SUBCMD_S_Connect();
		String sData = cmd.getJsonData();
		agent.SendMessage(sData);
	}

	public static void Handle_onJoin(GameAgent agent, String[] vecArg) {
		SUBCMD_S_Join cmd = new SUBCMD_S_Join();
		String sData = cmd.getJsonData();
		agent.SendMessage(sData);
	}

	public static void Handle_onLeave(GameAgent agent, String[] vecArg) {
		
		Integer _Uid = agent.getUid();
		MajTable pTable = (MajTable)FindTable(_Uid);
		
		if (pTable != null)
		{
			pTable.OnUserStandUp(_Uid,true); 
			
			
			QpGameMgr.getInstance().Handle_OnUserRoom(agent,new JSONObject());
			
		}
		
		//m_mapHeartTime.remove(_Uid);
		
	}

	public static void Handle_onDismissRoom(GameAgent agent, String[] vecArg) {
		Integer _Uid = agent.getUid();
		MajTable tbl = (MajTable)FindTable(_Uid);
		if(tbl != null)
		{
			QpGameMgr.getInstance().NotifyRemoveUserRoom(tbl);	
		}
		if(tbl != null && tbl.m_pIUserItem[0].dwUserID == _Uid && tbl.m_bTableStarted == false)
		{
			tbl.OnEventGameEnd(0, MajTable.GAME_END_DISMISS);
		}
	}
	 
	//创建房间
     // strParam: daidapai;1FEN;ipcheck;gpscheck;ganghoux4;mingangang;tianluodihu;yipaosanxiang;8JU;AAPAY;yunxuzhanzhaung                                      
	public static MajTable CreateTable(int _Uid, String strParam, boolean isFangGuan) {
		try {
			int byCostGem = 1;
			SHMUser user = dthall.getInstance().getUserInfo(_Uid);
			if (user == null)
				return null;

			if (strParam == null)
				return null;

			String[] vecParam = strParam.split(";");

			if (vecParam.length == 0)
				return null;

			//选择局数
			int jushu = 0;		
			String strRoomDesc = "";
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("4JU")) {
					jushu = 1;
					break;
				}
				if (param.equalsIgnoreCase("8JU")) {
					jushu = 2;
					break;
				}
				if(param.equalsIgnoreCase("12JU")){
					jushu = 12;				
					break;
				}
				if (param.equalsIgnoreCase("16JU")) {
					jushu = 16;
					break;
				}
			}
			
			//选择胡法
			int hufa = 0;		
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("DAJIAZI")) {
					hufa = 1;
					break;
				}
				if (param.equalsIgnoreCase("LIANGTOU")) {
					hufa = 0;
					break;
				}
			}
			
			//选择大头倍数
			int datoubeishu = 0;		
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("daTouBeishu0")) {
					datoubeishu = 0;
				}
				if (param.equalsIgnoreCase("daTouBeishu1")) {
					datoubeishu = 1;
				}
				if (param.equalsIgnoreCase("daTouBeishu2")) {
					datoubeishu = 2;
				}
				if (param.equalsIgnoreCase("daTouBeishu3")) {
					datoubeishu = 3;
				}
				if (param.equalsIgnoreCase("daTouBeishu4")) {
					datoubeishu = 4;
				}
				if (param.equalsIgnoreCase("daTouBeishu5")) {
					datoubeishu = 5;
				}			
			}
			
			//选择小头倍数
			int xiaotoubeishu = 0;		
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("xiaoTouBeishu0")) {
					xiaotoubeishu = 0;
				}
				if (param.equalsIgnoreCase("xiaoTouBeishu1")) {
					xiaotoubeishu = 1;
				}
				if (param.equalsIgnoreCase("xiaoTouBeishu2")) {
					xiaotoubeishu = 2;
				}
				if (param.equalsIgnoreCase("xiaoTouBeishu3")) {
					xiaotoubeishu = 3;
				}
				if (param.equalsIgnoreCase("xiaoTouBeishu4")) {
					xiaotoubeishu = 4;
				}
				if (param.equalsIgnoreCase("xiaoTouBeishu5")) {
					xiaotoubeishu = 5;
				}
			}
			
			//选择小头倍数
			int gezuibeishu = 0;		
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("geZuiBeishu0")) {
					gezuibeishu = 0;
				}
				if (param.equalsIgnoreCase("geZuiBeishu1")) {
					gezuibeishu = 1;
				}
				if (param.equalsIgnoreCase("geZuiBeishu2")) {
					gezuibeishu = 2;
				}
				if (param.equalsIgnoreCase("geZuiBeishu3")) {
					gezuibeishu = 3;
				}
				if (param.equalsIgnoreCase("geZuiBeishu4")) {
					gezuibeishu = 4;
				}
				if (param.equalsIgnoreCase("geZuiBeishu5")) {
					gezuibeishu = 5;
				}
			}
			
			//选择底分
			int difenbeishu = 0;		
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("diFenBeishu0")) {
					difenbeishu = 1;
				}
				if (param.equalsIgnoreCase("diFenBeishu1")) {
					difenbeishu = 1;
				}
				if (param.equalsIgnoreCase("diFenBeishu2")) {
					difenbeishu = 2;
				}
				if (param.equalsIgnoreCase("diFenBeishu3")) {
					difenbeishu = 3;
				}
				if (param.equalsIgnoreCase("diFenBeishu4")) {
					difenbeishu = 4;
				}
				if (param.equalsIgnoreCase("diFenBeishu5")) {
					difenbeishu = 5;
				}
			}
			//选择支付方式
			int isMPHu = 0;
			String payway = "AAPAY";
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if (param.equalsIgnoreCase("FZPAY")) {
					payway = "FZPAY";
				}
				if (param.equalsIgnoreCase("AAPAY")) {
					payway = "AAPAY";
				}
				if(param.equalsIgnoreCase("WINPAY")){
					payway = "WINPAY";
				}
				if(param.equalsIgnoreCase("CLUBPAY")){
					payway = "CLUBPAY";
				}
			}
			
			//选择跑嘴方式 equalsIgnoreCase忽略大小写
			String pao = "BUPAOZUI";
			int paofen=0;
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				//不跑嘴
				if (param.equalsIgnoreCase("BUPAOZUI")) {
					pao = "BUPAOZUI";
					paofen=0;
				}
			   //自由跑嘴
				if (param.equalsIgnoreCase("ZIYOUPAOZUI")) {
					pao = "ZIYOUPAOZUI";
					paofen=1;
				}
				//固定跑嘴
				if(param.equalsIgnoreCase("GUDINGPAOZUI")){
					pao = "GUDINGPAOZUI";
					paofen=2;
				}
			}
			
			int diFenNum = 1;		
			int fdFen = 1;
			int maxCost = byCostGem;
			if(payway.equals("AAPAY")){
				if(jushu==16){
					maxCost = 15;
				}				 
				if(jushu==8){
					maxCost = 8;
				}
				if(jushu==4){
					maxCost = 4;
				}
			}
			if(payway.equals("FZPAY") || payway.equals("CLUBPAY")){
				if(jushu==16){
					maxCost = 60;
				}				
				if(jushu==8){
					maxCost = 30;
				}
				if(jushu==4){
					maxCost = 15;
				}
			}

			int realCost = maxCost;
			if(payway.equals("CLUBPAY")){
				maxCost = 0;
			}
			System.out.println("1[" + maxCost +"]1");
			boolean ipLimit = false;
			boolean gpsLimt = false;
			int inZuanShi = 0;	
			boolean daidapai = false;
			boolean shuidashuibao = false;		
			boolean qianggangzimo = true;
			boolean ganghoux4 = false;
			boolean mingangang = false;
			boolean tianluodihu = false;
			boolean yipaosanxiang = false;
			boolean yunxuzhanzhaung=false;
			boolean buzhunchipai=false;
			boolean qiangGangHu=false;
			boolean Pao=false;
			 
			for (int i = 0; i < vecParam.length; i++) {
				String param = vecParam[i];
				if(param.equalsIgnoreCase("ipcheck"))
					ipLimit = true;
				else if(param.equalsIgnoreCase("gpscheck"))
					gpsLimt = true;
				else if(param.startsWith("zhunru:")){
					String numStr = param.replace("zhunru:","");
					inZuanShi = Integer.parseInt(numStr);
				}else if(param.equalsIgnoreCase("daidapai"))
					daidapai = true;
				else if(param.equalsIgnoreCase("qiangangzimo"))
					qianggangzimo = true;
				else if(param.equalsIgnoreCase("GHCSB"))
					ganghoux4 = true;
				else if(param.equalsIgnoreCase("YPDX"))
					yipaosanxiang = true;
				else if(param.equalsIgnoreCase("TLDH"))
					tianluodihu = true;
				else if(param.equalsIgnoreCase("mingangang"))
					mingangang = true;
				else if(param.equalsIgnoreCase("SDSCF"))
					shuidashuibao = true;
				else if(param.equalsIgnoreCase("YXLZ"))
					yunxuzhanzhaung = true;	
				else if(param.equalsIgnoreCase("BZCP"))
					buzhunchipai = true;
				else if(param.equalsIgnoreCase("QGH"))
					qiangGangHu=true;
				else if(param.equalsIgnoreCase("ZIYOUPAOZUI"))
					Pao=true;
				else if(param.equalsIgnoreCase("GUDINGPAOZUI"))
					Pao=true;
				else if(param.equalsIgnoreCase("BUPAOZUI"))
					Pao=true;
			}
			if(inZuanShi != 0 && user.getGem(0) < inZuanShi){
				//准入钻石不够
				return null;			
				}
			//if (jushu > 0 && payway.length() > 0) {			
			if (jushu > 0 && byCostGem > 0 && payway.length() > 0 && user.getGem(0) >= maxCost) {				
				int factor = 0;
				for (int i = 0; i < 1000000; i++) {
                    //生成放好
					int rand_room = GlobalSetting.getInstance().getRandRoomNum();//Constants.rand(100000, 999999, factor);
					//int rand_room=0;
					factor = rand_room;
					MajTable room = (MajTable) QpGameMgr.getInstance().getRoomById(rand_room);
					if (room == null) {
						room = new MajTable();
						room.Init(Constants.INIT_TOTAL);
						room.SetParam(strParam);
						room.SetDesc(strRoomDesc);
						// room.setAppHandler(m_objAppHandler);
						room.m_diFen = diFenNum;
						room.m_MPHu = isMPHu;
//						room.m_fdFen = fdFen;
						room.m_dwTableID = rand_room;
						room.m_byCostGem = byCostGem;
						room.m_maxCostGem = realCost;
						room.m_payWay = payway;
						room.m_datoubeishu = datoubeishu;
						room.m_xiaotoubeishu=xiaotoubeishu;
						room.m_gezuibeishu=gezuibeishu;
						room.m_difenbeishubeishu = difenbeishu;
						room.m_byMaxRound = (int) jushu;
						room.m_Creator = isFangGuan?_Uid:0;
						
						room.m_hqQiangGangZiMo = qianggangzimo;
						room.m_hqGangHoux4 = ganghoux4;
						room.m_hqYiPaoSanXiang = yipaosanxiang;
						room.m_hqTianLuoDiHu = tianluodihu;
						
						room.m_hqIpLimit = ipLimit;
						room.m_hqGpsLimit = gpsLimt;
						room.m_hqInZuanNum = inZuanShi;
						room.m_hufa = hufa;
						/**   
						 * 判断是否跑嘴
						 */
						room.m_paoZui=Pao;
						/**
						 * 判断是否抢杠胡
						 */
//						room.m_qiangGangHu=qiangGangHu;
					   /**
					    * 判断玩法
					    */
						room.m_paoType=paofen;
						
						QpGameMgr.getInstance().putRoomIntoMap(rand_room, room);

						return room;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	void DismissTable(MajTable pTable) {

		if (pTable == null)
			return;

		QpGameMgr.getInstance().OnDismissRoom((IRoom) pTable);

		{

			m_mapTable.remove(pTable.GetID());
		}

	}

	void OnDeal(float dt) {
		Long tnow_ms = getCurrentTimeMillis();
		if (m_LastDealTime == 0) {
			m_LastDealTime = tnow_ms;
		}

		if (tnow_ms - m_LastDealTime >= 1000) {
			m_LastDealTime = tnow_ms;

		}
	}

	private Long getCurrentTimeMillis() {
		return System.currentTimeMillis();
	}

	public static void OnRecv(GameAgent agent, JSONObject root) {
		String data = root.optString("data");
		String vecArg2[] = data.split("\\|");
		if (vecArg2.length == 0){
			return;
		}
		String subcmd = vecArg2[0];
		switch (subcmd) {
		case "connect":
			Handle_onConnect(agent,vecArg2);
			return;
		case "join":
			Handle_onJoin(agent,vecArg2);
			return;
		case "leave":
			Handle_onLeave(agent,vecArg2);
			return;
		case "dismissroom":
			Handle_onDismissRoom(agent,vecArg2);
			return;	
//		case "kick":
//			Handle_onKick(agent,vecArg2);
//			return;
		case "number":
			Handle_onNumber(agent,vecArg2);
			return;
		default:
			break;
		}
		
		Integer lUid = agent.getUid();
		MajTable tbl = (MajTable)FindTable(lUid);
		if (tbl == null) {
			return;
		}
		
		   if (subcmd.equals("reload")) {
			tbl.Handle_onReload(lUid, vecArg2);
		} else if (subcmd.equals("try_dismissroom")) {
			tbl.Handle_onTryDismissRoom(lUid, vecArg2);
		} else if (subcmd.equals("agree_dismissroom")) {
			tbl.Handle_onAgreeDismissRoom(lUid, vecArg2);
		} else if (subcmd.equals("refuse_dismissroom")) {
			tbl.Handle_onRefuseDismissRoom(lUid, vecArg2);
		}
		
		boolean printLog = true;
		if (subcmd.equals("ready")) {
			tbl.Handle_onReady(lUid, vecArg2);
		} else if (subcmd.equals("robot")) {
			tbl.Handle_onRobot(lUid, vecArg2);
		} else if (subcmd.equals("biaoqing")) {
			tbl.Handle_onBiaoQing(lUid, vecArg2);
		} else if (subcmd.equals("yuyin")) {
			tbl.Handle_onYuYin(lUid, vecArg2);
		} else if (subcmd.equals("chat")) {
			tbl.Handle_onChat(lUid, vecArg2);
		} else if (subcmd.equals("voice")) {
			tbl.Handle_onVoice(lUid, vecArg2);
		} else if (subcmd.equals("voicemsg")) {
			printLog = false;
			tbl.Handle_onVoiceMsg(lUid, vecArg2);
		} else if (subcmd.equals("text")) {
			tbl.Handle_onText(lUid, vecArg2);
		} else if (subcmd.equals("try_mopai")) {
			tbl.Handle_onTryMoPai(lUid, vecArg2);
		} else if (subcmd.equals("mopai")) {
			tbl.Handle_onMoPai(lUid, vecArg2);
		} else if (subcmd.equals("chupai")) {
			tbl.Handle_onChuPai(lUid, vecArg2);
		} else if (subcmd.equals("chipai")) {
			tbl.Handle_onChiPai(lUid, vecArg2);
		} else if (subcmd.equals("pengpai")) {
			tbl.Handle_onPengPai(lUid, vecArg2);
		} else if (subcmd.equals("gangpai")) {
			tbl.Handle_onGangPai(lUid, vecArg2);
		} else if (subcmd.equals("guopai")) {
			tbl.Handle_onGuoPai(lUid, vecArg2);
		} else if (subcmd.equals("hupai")) {
			tbl.Handle_onHuPai(lUid, vecArg2);
		} else if (subcmd.equals("baozi")){
//			tbl.Handle_onBaoZiPai(lUid,vecArg2);
		} else if (subcmd.equals("inBack")){
			tbl.Handle_InBack(lUid,vecArg2);
		} else if (subcmd.equals("inFore")){
			tbl.Handle_InFore(lUid,vecArg2);
		} else if (subcmd.equals("clientIp")){
			tbl.Handle_ClientIp(lUid,vecArg2);
		} else if (subcmd.equals("clientGps")){
			tbl.Handle_ClientGps(lUid,vecArg2);
		} else if (subcmd.equals("pao")){
			tbl.Handle_onPao(lUid,vecArg2);
		} else if (subcmd.equals("lao")){
			tbl.Handle_onLao(lUid,vecArg2);
		} else if (subcmd.equals("kick")){
			tbl.Handle_Kick(lUid, vecArg2);
		}
		
		if(printLog)
			System.out.println("Msg=" + root.toString());
	}

	private static void Handle_onNumber(GameAgent agent, String[] vecArg2) {
		 Integer number=Integer.parseInt(vecArg2[1])+1;
		  SUBCMD_S_Number ssm=new SUBCMD_S_Number();
		  ssm.number=number;
		  String data =ssm.getJsonData();
		  agent.SendMessage(data);
	}

	  

	private static void Handle_onKick(GameAgent agent, String[] vecArg2) {
		/*
		Integer _Uid = Integer.parseInt(vecArg2[1]);
		
		GameAgent agent2  =  GameAgent.find_client_by_user_id(_Uid);
		MajTable pTable = (MajTable)FindTable(_Uid);
		
		if (pTable != null)
		{
			pTable.OnUserStandUp(_Uid); 
		}
		
		SUBCMD_S_Kick cmd = new SUBCMD_S_Kick();
		cmd.m_lUid = _Uid;
		//{"mjmsg":"kick|31448|0|0","command":"mjmsg"}
		String sData = cmd.getJsonData();
		agent2.Broadcast(sData);
		*/
	}
}
